<?php

include_once dirname(__FILE__) . "/../error.php";
include_once dirname(__FILE__) . "/../log.php";
include_once dirname(__FILE__) . "/../open.php";

Log::init("Open_test");

class Open_test extends PHPUnit_Framework_TestCase
{

    const OPEN_QQ = "/home/david/soft/nginx/www/v1/test/.qq.dat";
    
    public function test_init_with_conf_error()
    {
        global $err_code;
        
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file format");

        $open = new Open();
        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
        );
        $ret = $open->init();
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_conf_error);

        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_get_info_with_with_invalid_arg()
    {
        global $err_code;
        $open = new Open();
        $arg = array();
        $ret = $open->get_info($arg);
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_param_error);
    }
    
    public function test_get_info_with_curl_failed()
    {
        global $err_code;
        $req = array(
            "open_id" => "abc", 
        );
        $rsp = array();
        $open_qq = array(
            json_encode($req) => $rsp,
        );
        file_put_contents(self::OPEN_QQ, serialize($open_qq));
        
        $open = new Open();
        $arg = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
        );
        $ret = $open->init();
        $this->assertTrue($ret);
        $ret = $open->get_info($arg);
        //print_r($ret);
        $this->assertTrue(gettype($ret) == "array");
        $this->assertTrue($ret['code'] != 0);
    }
    public function test_get_info_ok()
    {
        global $err_code;
        
        $open = new Open();
        $req = array(
            "open_id" => "abc", 
            "openkey" => "abc123", 
            "sig" => "def132", 
            "pf" => "t.qq.com",
            "interface" => "get_info",
        );
        $rsp = array(
            "code" => 0,
            "msg" => "okkkkk",
            "nickname" => "test",
            "province" => "广东",
            "city" => "深圳",
        );
        $open_qq = array(
            json_encode($req) => $rsp,
        );
        file_put_contents(self::OPEN_QQ, serialize($open_qq));

        $ret = $open->init();
        $this->assertTrue($ret);
        $ret = $open->get_info($req);
        //print_r($ret);
        $this->assertTrue($ret != false);
        $this->assertTrue($ret['code'] == 0);
    }
    /*
    */
}
?>
